<template>
  <div class="xtx-form">
      <Form
    ref="target"
    :validation-schema="mySchema"
    v-slot="{ errors }"
    autocomplete="off"
    class="xtx-form"
  >
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
          <Field
          :class="{ err: errors.account }"
          v-model="form.account"
          name="account"
          class="input"
          type="text"
          placeholder="请输入用户名"
        />
        <!-- <input class="input" type="text" placeholder="请输入用户名" /> -->
      </div>
      <div v-if="errors.account" class="error">{{ errors.account }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
         <Field
          :class="{ err: errors.mobile }"
          v-model="form.mobile"
          name="mobile"
          class="input"
          type="text"
          placeholder="请输入手机号"
        />
        <!-- <input class="input" type="text" placeholder="请输入手机号" /> -->
      </div>
      <div v-if="errors.mobile" class="error">{{ errors.mobile }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
          <Field
          :class="{ err: errors.code }"
          v-model="form.code"
          name="code"
          class="input"
          type="text"
          placeholder="请输入验证码"
        />
        <!-- <input class="input" type="text" placeholder="请输入验证码" /> -->
        <span class="code" @click="send"> {{countTime===0?'发送验证码':`${countTime}秒后发送`}}</span>
      </div>
      <div v-if="errors.code" class="error">{{ errors.code }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
          <Field
          :class="{ err: errors.password }"
          v-model="form.password"
          name="password"
          class="input"
          type="text"
          placeholder="请输入密码"
        />
        <!-- <input class="input" type="password" placeholder="请输入密码" /> -->
      </div>
      <div v-if="errors.password" class="error">{{ errors.password }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
          <Field
          :class="{ err: errors.rePassword }"
          v-model="form.confirm"
          name="rePassword"
          class="input"
          type="text"
          placeholder="请确认密码"
        />
        <!-- <input class="input" type="password" placeholder="请确认密码" /> -->
      </div>
      <div v-if="errors.rePassword" class="error">{{ errors.rePassword }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
      </Form>
  </div>
</template>

<script>
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import { useCountDown } from '@/compositions'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String
    }
  },
  components: {
    Form,
    Field
  },
  setup (props) {
    const form = {
      account: '',
      password: '',
      code: '',
      mobile: '',
      confirm: ''
    }
    // 校验规则对象
    const mySchema = {
      mobile: schema.mobile,
      code: schema.code,
      password: schema.password,
      account: schema.account,
      rePassword: schema.rePassword
    }
    // 点击验证码进行校验手机号和发送验证码
    const { countTime, start } = useCountDown(60)
    function send () {
      const valid = mySchema.mobile(form.mobile)
      if (valid === true) {
        if (countTime.value === 0) {
          userQQPatchCode(form.mobile).then(res => {
            start()
          })
        }
      }
    }

    // 兜底验证
    const target = ref(null)
    const store = useStore()
    const router = useRouter()
    async function submit () {
      const ref = await target.value.validate()
      if (ref) {
        userQQPatchLogin({
          unionId: props.unionId,
          mobile: form.mobile,
          code: form.code,
          account: form.account,
          password: form.password
        }).then(res => {
          console.log(res)
          store.commit('user/setUser', res.data.result)
          router.push('/login')
        }).catch(error => {
          console.log(error)
        })
      }
    }
    return {
      form,
      mySchema,
      send,
      submit,
      target,
      countTime
    }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
